#include <stdio.h>
#include <windows.h>

#include "winres.h"
#include "canvas.h"

static HINSTANCE globalInstance;

enum OpenSaveWhat {
	OS_SAVE,
	OS_SAVEAS
};

BOOL APIENTRY
aboutFunc(HWND hDlg, UINT message, UINT wParam, LONG lParam)
{
  switch (message) {
     case WM_INITDIALOG:
        break;
     case WM_CLOSE:
        {
           EndDialog(hDlg, TRUE);
           return (TRUE);
        }
     case WM_COMMAND:
            {
            switch (LOWORD(wParam)) {
               case IDOK:
                  EndDialog(hDlg, TRUE);
                  return TRUE;
               case IDCANCEL:
                  EndDialog(hDlg, TRUE);
                  return TRUE;
            }
          }
      }
      return FALSE;
}

static void setupMainWindow(HWND hWnd)
{
	canvasCreate(hWnd, 0, 0, 200, 200);
}

static void resizeWindow(HWND hWnd)
{
	RECT rect;
	GetClientRect(hWnd, &rect);
	canvasResize(rect.left, rect.top,
			rect.right, rect.bottom);
}


LRESULT CALLBACK
windowProc(HWND hWnd, UINT message, WPARAM wp, LPARAM lp)
{
	switch(message)
	{
	case WM_CREATE:
		setupMainWindow(hWnd);
		break;
	case WM_SIZE:
		resizeWindow(hWnd);
		break;
	case WM_CLOSE:
		PostQuitMessage(0);
		break;
	case WM_COMMAND:
		switch (wp)
		{
		case IDM_HELP_ABOUT:
			DialogBox(globalInstance, MAKEINTRESOURCE(1),
					hWnd, aboutFunc);
			break;
		case IDM_FILE_EXIT:
			SendMessage(hWnd, WM_CLOSE, 0, 0);
			break;
		}
		break;
	default:
		return DefWindowProc(hWnd, message, wp, lp);
	}
	return 0;
}

void createWindow()
{
	HINSTANCE hInstance = globalInstance;
	char *className = "DrawWindowClass";
	WNDCLASS wc;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = windowProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(2));
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = MAKEINTRESOURCE(4);
	wc.lpszClassName = className;

	RegisterClass(&wc);

	HWND wnd = CreateWindow(className, "Drap: a drawing application",
			WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
			CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
	ShowWindow(wnd, SW_SHOWNORMAL);
	UpdateWindow(wnd);
}

int main()
{
	globalInstance = GetModuleHandle(0);
	MSG msg;
	createWindow();
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}
